package hipro.event;

import hipro.Services;

import java.lang.reflect.Type;
import java.util.ArrayList;

import org.ksoap2.serialization.SoapObject;
import org.w3c.dom.Element;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;


public class Events {
	// name space for instance soup Object
	private String namespace = "";
	// "http://171.243.204.2/DetaiAndroid/public/users-soap";
	// URL of web service
	private String url = "";
	// "http://171.243.204.2/DetaiAndroid/public/users-soap";

	private Services services = null;

	public Events() {
		this.services = new Services();
	}

	public void setWebservice(String webservice) {
		this.namespace = webservice;
		this.url = webservice;
	}
	
	/**
	 * Get event category 
	 * @param username
	 * @param password
	 * @param categoryId
	 */
	public hipro.entities.EventCategories getCategoryById(String username, 
			String password, int categoryId){
		// create object soap object with name space and method.
		SoapObject soapObject = new SoapObject(this.namespace, "getCategoryById");
		// set parameters for function in method that web service will call.
		soapObject.addProperty("username", username);
		soapObject.addProperty("password", password);
		soapObject.addProperty("categoryId", categoryId);
		
		String json = this.services.call(this.url, "getCategoryById", soapObject);
		hipro.entities.EventCategories eventCategory = 
			new hipro.entities.EventCategories();
		Gson gson = new Gson();
		eventCategory =  gson.fromJson(json, hipro.entities.EventCategories.class);
		
		return eventCategory;
	}

	/**
	 * Load list events by user name and password, user name and password to
	 * login system. latitude and longtitude will load some events have distance
	 * <= distance of user to events.
	 * 
	 * @param username
	 * @param password
	 * @param lat
	 * @param lng
	 * @return String
	 */
	public ArrayList<hipro.entities.Events> listEvents(String username, String password, String lat,
			String lng) {

		// create object soap object with name space and method.
		SoapObject soapObject = new SoapObject(this.namespace, "listEvents");
		// set parameters for function in method that web service will call.
		soapObject.addProperty("username", username);
		soapObject.addProperty("password", password);
		soapObject.addProperty("lat", lat);
		soapObject.addProperty("lng", lng);

		String json = this.services.call(this.url, "listEvents", soapObject);
		ArrayList<hipro.entities.Events> arrList = new ArrayList<hipro.entities.Events>();
		Gson gson = new Gson();
		Type collectionType = new TypeToken<ArrayList<hipro.entities.Events>>(){}
			.getType();
		arrList =  gson.fromJson(json, collectionType);

		return arrList;
	}
	
	
	public ArrayList<hipro.entities.Events> listEventsByUserId(String username, 
			String password, int userId, int start, int limit) {

		// create object soap object with name space and method.
		SoapObject soapObject = new SoapObject(this.namespace, "listEventsByUserId");
		// set parameters for function in method that web service will call.
		soapObject.addProperty("username", username);
		soapObject.addProperty("password", password);
		soapObject.addProperty("userId", userId);
		soapObject.addProperty("start", start);
		soapObject.addProperty("limit", limit);

		String json = this.services.call(this.url, "listEventsByUserId", soapObject);
		ArrayList<hipro.entities.Events> arrList = new ArrayList<hipro.entities.Events>();
		Gson gson = new Gson();
		Type collectionType = new TypeToken<ArrayList<hipro.entities.Events>>(){}
			.getType();
		arrList =  gson.fromJson(json, collectionType);

		return arrList;
	}
	
	public ArrayList<hipro.entities.EventCategories> getCategory(
			String username, String password){
		return this.listEventCategories(username, password);
	}
	
	/**
	 * Show list event categories
	 * @author NguyenVanTien
	 * @param username
	 * @param password
	 * @return
	 */
	public ArrayList<hipro.entities.EventCategories> listEventCategories(
			String username, String password){
		
		ArrayList<hipro.entities.EventCategories> arrList = 
			new ArrayList<hipro.entities.EventCategories>();
		
		// create object soap object with name space and method.
		SoapObject soapObject = new SoapObject(this.namespace, "listEventCategories");
		// set parameters for function in method that web service will call.
		soapObject.addProperty("username", username);
		soapObject.addProperty("password", password);

		String json = this.services.call(this.url, "listEventCategories", soapObject);
		
		Gson gson = new Gson();
		Type collectionType = new TypeToken<ArrayList<hipro.entities.EventCategories>>(){}
			.getType();
		arrList =  gson.fromJson(json, collectionType);
		
		return arrList;
	}
	

//	private ArrayList<hipro.entities.Events> getListEventFromXml(String xml) {
//		ArrayList<hipro.entities.Events> arrList = new ArrayList<hipro.entities.Events>();
//		try {
//			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
//					.newInstance();
//			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
//			InputSource is = new InputSource();
//			is.setCharacterStream(new StringReader(xml));
//			
//			//inputSource.
//			Document doc = dBuilder.parse(is);
//			doc.getDocumentElement().normalize();
//			//SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yy");
//			
//			//get list by root name
//			NodeList nList = doc.getElementsByTagName("row");
//			
//			for (int temp = 0; temp < nList.getLength(); temp++) {
//				Node nNode = nList.item(temp);
//				
//				if (nNode.getNodeType() == Node.ELEMENT_NODE) {
//					Element eElement = (Element) nNode;
//					
//					//create events
//					hipro.entities.Events events = new hipro.entities.Events();
//					events.setEventId(Integer.parseInt(getTagValue("event_id", eElement)));
//					events.setEventName(getTagValue("event_name", eElement));
//					events.setLat(getTagValue("lat", eElement));
//					events.setLng(getTagValue("lng", eElement));
//					events.setCreatedDate(getTagValue("created_date", eElement));
//					events.setEventDescriptions(getTagValue("event_descriptions", eElement));
//					events.setEventCategoryId(Integer.parseInt(getTagValue("event_category_id", eElement)));
//					
//					arrList.add(events);
//				}
//			}
//		} catch (Exception e) {
//			Log.d("Tien", "Exception debug "+ e.toString());
//			System.out.println(e.toString());
//		}
//
//		return arrList;
//	}
	
	public String addEvent(String username, String password, hipro.entities.Events events){
		Gson gson = new Gson();
		
		SoapObject soapObject = new SoapObject(this.namespace, "addEvent");
		// set parameters for function in method that web service will call.
		soapObject.addProperty("username", username);
		soapObject.addProperty("password", password);
		soapObject.addProperty("data", gson.toJson(events));

		String res = this.services.call(this.url, "addEvent", soapObject);
		return res;
	}
	
	/**
	 * Remove event by event id
	 * @param username
	 * @param password
	 * @param eventId
	 * @return
	 */
	public boolean removeEvent(String username, String password, 
			int eventId){
		SoapObject soapObject = new SoapObject(this.namespace, "removeEvent");
		// set parameters for function in method that web service will call.
		soapObject.addProperty("username", username);
		soapObject.addProperty("password", password);
		soapObject.addProperty("eventId", eventId);

		String res = this.services.call(this.url, "removeEvent", soapObject);
		return Boolean.parseBoolean(res);
	}

	public hipro.entities.Events getEvent(String username,
			String password, int eventId){
		
		Gson gson = new Gson();
		
		SoapObject soapObject = new SoapObject(this.namespace, "getEvent");
		// set parameters for function in method that web service will call.
		soapObject.addProperty("username", username);
		soapObject.addProperty("password", password);
		soapObject.addProperty("eventId", eventId);

		String res = this.services.call(this.url, "getEvent", soapObject);
		hipro.entities.Events event = gson.fromJson(res, hipro.entities.Events.class);
		
		return event;
	}
	
	/**
	 * Update event
	 * @param username
	 * @param password
	 * @param event
	 * @return
	 */
	public String updateEvent(String username, String password,
			hipro.entities.Events event){
		return this.editEvent(username, password, event);
	}
	
	/**
	 * 
	 * @param username
	 * @param password
	 * @param data
	 * @return
	 */
	public String editEvent(String username, String password, 
			hipro.entities.Events event){
		Gson gson = new Gson();
		
		SoapObject soapObject = new SoapObject(this.namespace, "editEvent");
		// set parameters for function in method that web service will call.
		soapObject.addProperty("username", username);
		soapObject.addProperty("password", password);
		soapObject.addProperty("data", gson.toJson(event));
		
		String res = this.services.call(this.url, "editEvent", soapObject);
		
		return res;
		//return Boolean.parseBoolean(res);
	}
	
	/**
	 * Get value of tag in XML
	 * @author Tien Nguyen
	 * @param sTag
	 * @param eElement
	 * @return string
	 */
	private static String getTagValue(String sTag, Element eElement) {
		return eElement.getElementsByTagName(sTag).item(0).getFirstChild().getNodeValue();
	}
}
